export const redisConfig = {
  options: {
    host: process.env.REDIS_HOST || 'localhost',
    port: Number(process.env.REDIS_PORT) || 6379,
    password: process.env.REDIS_PASSWORD,
    db: Number(process.env.REDIS_DB) || 0,

    // 连接配置
    connectTimeout: 10000,
    maxRetriesPerRequest: 3,
    enableReadyCheck: true,

    // 集群配置
    cluster: process.env.REDIS_CLUSTER === 'true',
    clusterRetryStrategy: (times: number) => {
      const delay = Math.min(100 + times * 2, 2000)
      return delay
    },

    // 哨兵配置
    sentinels: process.env.REDIS_SENTINELS
      ? process.env.REDIS_SENTINELS.split(',').map((s) => {
          const [host, port] = s.split(':')
          return { host, port: Number(port) }
        })
      : undefined,
    sentinelPassword: process.env.REDIS_SENTINEL_PASSWORD,

    // 性能优化
    keepAlive: 30000,
    connectionName: 'live-platform',
    autoResubscribe: true,
    autoResendUnfulfilledCommands: true,
    maxLoadingRetryTime: 2000,

    // TLS配置
    tls:
      process.env.REDIS_TLS === 'true'
        ? {
            rejectUnauthorized: false,
          }
        : undefined,
  },

  // 键前缀配置
  keyPrefix: {
    user: 'user:',
    room: 'room:',
    chat: 'chat:',
    gift: 'gift:',
    rank: 'rank:',
    stream: 'stream:',
  },

  // 缓存TTL配置
  ttl: {
    userInfo: 3600, // 用户信息缓存1小时
    roomInfo: 300, // 房间信息缓存5分钟
    chatHistory: 86400, // 聊天历史保存1天
    giftRanking: 3600, // 礼物排行榜1小时
    streamStatus: 60, // 直播状态1分钟
    verifyCode: 300, // 验证码5分钟
  },
}
